﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OOAD_Project_Engine.Foundation.Interfaces;
using OOAD_Project_Engine.Foundation.MyEventArgs;

namespace OOAD_Project_Engine.Domain.Components
{
    public abstract class EntityComponent : IEntityComponent
    {
        IEntity owner;
        int updateOrder;
        bool enabled = true;

        public event EventHandler<EntityEventArgs> OwnerChanged;

        protected virtual void OnOwnerChanged(EntityEventArgs e)
        {
            if (OwnerChanged != null)
            {
                OwnerChanged(this, e);
            }
        }

        public IEntity Owner
        {
            get
            {
                return owner;
            }
            set
            {
                if (owner == null)
                {
                    owner = value;
                    OnOwnerChanged(new EntityEventArgs(value));
                }
            }
        }

        public event EventHandler EnabledChanged;

        protected virtual void OnEnabledChanged(object sender, EventArgs args)
        {
            if (EnabledChanged != null)
            {
                EnabledChanged(this, EventArgs.Empty);
            }
        }

        public virtual void Update(Microsoft.Xna.Framework.GameTime gameTime) { }

        public virtual bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                if (enabled != value)
                {
                    enabled = value;

                    OnEnabledChanged(this, EventArgs.Empty);
                }
            }
        }

        public int UpdateOrder
        {
            get
            {
                return updateOrder;
            }
            set
            {
                if (updateOrder != value)
                {
                    updateOrder = value;

                    OnUpdateOrderChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler UpdateOrderChanged;

        protected virtual void OnUpdateOrderChanged(EventArgs e)
        {
            if (this.UpdateOrderChanged != null)
            {
                this.UpdateOrderChanged(this, e);
            }
        }

        public void Dispose(){}

        public virtual void Initialize() { }

        public abstract object Clone();
    }
}
